<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/24
 * Time: 10:33
 */
namespace app\admin\controller;
use app\admin\model\Documents;
use app\admin\model\Models;
use app\admin\model\Subject;
use app\admin\model\UpFile;
use think\Controller;
use think\Db;
use think\Request;

class Huisou extends Controller
{
//    public function dshow_list(){
//        return NewBuilder::make('table')
////            ->addTableHeaderAllDel('huisou/huifu','全部恢复')
////            ->addTableHeaderSousuo()
//            ->addTableColumn('id','text','ID')
//            ->addTableColumn('name','text','名称')
//            ->addTableColumn('dir','text','描述')
//            ->addTableColumn('cid','text','栏目名称')
//            ->addTableRightHeader('操作')
//            ->addTableRight('button','huisou/dhuifu','恢复')
//            ->addTableRight('del','huisou/ddel','永久删除')
//            ->addTablePage('huisou/dpage')
//            ->fetch();
//    }
//
//    /**加载数据
//     * @param Request $request
//     * @return \think\response\Json
//     * @throws \think\db\exception\DataNotFoundException
//     * @throws \think\db\exception\ModelNotFoundException
//     * @throws \think\exception\DbException
//     */
//    public function dpage(Request $request){
//        $limit = $request->param('limit',10);
//        $data = Db::name(session('modular').'_document')
//            ->where('delete_time',1)
//            ->paginate($limit)
//            ->toArray();
//        foreach ($data['data'] as $key=>$value){
//            $column = Db::name(session('modular').'_column')->where('id',$value['cid'])->find();
//            $model = Models::where('id',$column['model_id'])->findOrEmpty()->toArray();
//            $shuju = Db::name($model['model_table'])->where('aid',$value['id'])->find();
//            $value['name'] = $shuju['name'];
//            $value['dir'] = $shuju['dir'];
//            $value['cid'] = $column['name'];
//            $data['data'][$key] = $value;
//        }
//        return json_page_response($data);
//    }
//
//    /**恢复
//     * @param Request $request
//     * @return \think\response\Json
//     * @throws \think\Exception
//     * @throws \think\exception\PDOException
//     */
//    public function dhuifu(Request $request){
//        $id = $request->param('id');
//        if(Db::name(session('modular').'_document')->where('id','in',$id)->update([
//            'delete_time'=>null
//        ])){
//            return json_my_response('恢复成功',200);
//        }else{
//            return json_my_response('恢复失败');
//        }
//    }
//
//    /**删除
//     * @param Request $request
//     * @return \think\response\Json
//     * @throws \think\Exception
//     * @throws \think\db\exception\DataNotFoundException
//     * @throws \think\db\exception\ModelNotFoundException
//     * @throws \think\exception\DbException
//     * @throws \think\exception\PDOException
//     */
//    public function ddel(Request $request){
//        $id = $request->param('id');
//        $data = Db::name(session('modular').'_document')
//            ->where('id',$id)
//            ->find();
//        $obj = new Subject();
//        $obj->setCid($data['cid']);
//        $model = $obj->getModel();
//        $field  = $obj->getField();
//        $change = Db::name($model['model_table'])
//            ->where('aid',$id)
//            ->find();
//        foreach ($field as $value){
//            if($value['form_type']=='单文件'||$value['form_type']=='单图片'||$value['form_type']=='多文件'||$value['form_type']=='多图片'){
//                if($change[$value['field']]){
//                    UpFile::delFile($change[$value['field']],true);
//                }
//            }
//        }
//        if(Db::name($model['model_table'])->where('aid',$id)->delete()&&Db::name(session('modular').'_document')
//                ->where('id',$id)->delete()){
//            return json_my_response('删除成功',200);
//        }else{
//            return json_my_response('删除失败',205);
//        }
//    }

    /**加载联表回收站
     * @return mixed
     */
    public function lshow_list(){
        return NewBuilder::make('table')
//            ->addTableHeaderAllDel('huisou/huifu','全部恢复')
//            ->addTableHeaderSousuo()
            ->addTableColumn('id','text','ID')
            ->addTableColumn('name','text','名称')
            ->addTableColumn('biao','text','表名称')
            ->addTableRightHeader('操作')
            ->addTableRight('button','huisou/lhuifu','恢复')
            ->addTableRight('del','huisou/ldel','永久删除')
            ->addTablePage('huisou/lpage')
            ->fetch();
    }

    /**加载数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lpage(Request $request){
        $limit = $request->param('limit',10);
        $page = $request->param('page',1);
        $data = [];
        $model1 = Models::where('model_type','单表模型')
            ->select()->toArray();
        $model2 = Models::where('model_type','联表模型')
            ->select()
            ->toArray();
        $model = array_merge($model1,$model2);
//        echo '<pre>';
//        print_r($model);
//        exit;
        foreach ($model as $value){
            $datas = Db::name($value['model_table'])
                ->where('delete_time',1)
                ->select();
            foreach ($datas as $key=>$va){
                $va['biao'] = $value['model_table'];
                $va['model_id'] = $value['id'];
                $datas[$key] = $va;
            }
            $data = array_merge($data,$datas);
        }
        $retu['data'] = array_slice($data,($page-1)*$limit,$limit);
        $retu['total'] = count($data);
        return json_page_response($retu);
    }

    /**恢复联表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function lhuifu(Request $request){
        $data = $request->param();
        $obj = new Subject();
        $obj->setModelId($data['model_id']);
        $model = $obj->getModel();
        if(Db::name($model['model_table'])->where('id',$data['id'])->update(['delete_time'=>null])){
            return json_my_response('恢复成功',200);
        }else{
            return json_my_response('恢复失败');
        }
    }

    /** 彻底删除
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function ldel(Request $request){
        $data = $request->param();
        $obj = new Subject();
        $obj->setModelId($data['model_id']);
        $model = $obj->getModel();
        $shuju = Db::name($model['model_table'])->where('id',$data['id'])->select();
        $obj->delFieldToFile($shuju);
        if(Db::name($model['model_table'])->where('id',$data['id'])->delete()){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }
    }
}